package cn.lzd.flag

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.io.Source

/**
  * Created by lzd on 2019/1/29.
  */
object NameAddFlag {
  def main(args: Array[String]): Unit = {

    val sc = new SparkContext(new SparkConf().setMaster("local").setAppName("company-name-flag"))

    val names: RDD[String] = sc.textFile("D:\\nametlx\\nameflag")

    val results: List[String] = Source.fromFile("C:\\Users\\lzd\\Desktop\\result").getLines().toList

    val map: RDD[String] = names.map((x) => {
      val arr: Array[String] = x.split("\\*\\|\\*", -1)
      //商号
      var index_0 = arr(0)
      //公司名
      var index_1 = arr(1)
      //公司名简写
      var index_2 = arr(2)
      //标签
      //var index_3 = arr(3)
      var builder = new StringBuilder
      builder.append(index_0).append("*|*").append(index_1).append("*|*").append(index_2).append("*|*")
      var tags = ""
      results.foreach((x) => {
        if (index_2.indexOf(x) != -1) {
          tags = tags + "_" + x
        }
      })
      if (tags.startsWith("_")) {
        tags = tags.substring(1)
      }
      builder.append(tags)
      builder.toString()
    })
    map.saveAsTextFile("D:\\nametlx\\sparkOut")
    sc.stop()
  }
}
